package com.ruoyi.performance.service.impl;

import com.google.common.base.Joiner;
import com.ruoyi.performance.domain.PerfLevelScore;
import com.ruoyi.performance.mapper.PerfLevelScoreMapper;
import com.ruoyi.performance.mapper.PerfLevelTemplateMapper;
import com.ruoyi.performance.service.PerfLevelScoreService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;

/**
 * @author YL
 * @date 2020/8/27 17:06
 * @description 绩效等级分数区间Service接口实现类
 */
@Service
@Slf4j
public class PerfLevelScoreServiceImpl implements PerfLevelScoreService {

    /**
     * 绩效模板初始状态
     */
    public static final String INITIAL ="New";

    
    @Resource
    private PerfLevelScoreMapper perfLevelScoreMapper;

    @Resource
    private PerfLevelTemplateMapper perfLevelTemplateMapper;

    /**
     * 根据id查找绩效等级分数区间
     *
     * @param id 绩效分数区间id
     * @return 绩效等级分数区间
     */
    @Override
    public PerfLevelScore selectPerfLevelScoreById(int id) {
        return perfLevelScoreMapper.selectPerfLevelScoreById(id);
    }

    /**
     * 查询绩效等级分数区间列表
     *
     * @return 绩效等级分数区间集合
     */
    @Override
    public List<PerfLevelScore> selectPerfLevelScoreList(PerfLevelScore perfLevelScore) {
        return perfLevelScoreMapper.selectPerfLevelScoreList(perfLevelScore);
    }

    /**
     * 新增绩效等级分数区间
     *
     * @param perfLevelScore 绩效等级分数区间
     * @return 结果
     */
    @Override
    public int insertPerfLevelScore(PerfLevelScore perfLevelScore) {
        return perfLevelScoreMapper.insertPerfLevelScore(perfLevelScore);
    }

    /**
     * 修改绩效等级分数区间
     *
     * @param perfLevelScore 绩效等级分数区间
     * @return 结果
     */
    @Override
    public int updatePerfLevelScore(PerfLevelScore perfLevelScore) {
        //获取对应的绩效模板的ID
        int levelTemplateId=perfLevelScoreMapper.selectPerfLevelScoreById(perfLevelScore.getId()).getLevelTemplateId();
        //获取对应的绩效模板的状态
        String status=perfLevelTemplateMapper.selectPerfLevelTemplateById(levelTemplateId).getStatus();
        System.out.println(status);
        //模板发布后禁止修改等级
        if (!status.equals(INITIAL)){
            return -1;
        }
        return perfLevelScoreMapper.updatePerfLevelScore(perfLevelScore);
    }

    /**
     * 删除绩效等级分数区间
     *
     * @param id 分数区间id
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String deletePerfLevelScoreById(String ids) {
    	
    	  String result = "1";
  		List<String> checkstatus = new ArrayList<String>();
  		String[] tmpids = ids.split(",");
  		for (String tmpid : tmpids) {
                  //转成int
  			int id = Integer.parseInt(tmpid); 
                  int levelTemplateId=perfLevelScoreMapper.selectPerfLevelScoreById(id).getLevelTemplateId();
                  //获取对应的绩效模板的状态
                  String status=perfLevelTemplateMapper.selectPerfLevelTemplateById(levelTemplateId).getStatus();
                  //模板发布后禁止删除等级
                  if (!status.equals(INITIAL)){
                      return "等级模板已发布，禁止删除!";
                  }
                  try {
					perfLevelScoreMapper.deletePerfLevelScoreById(id);
				} catch (Exception e) {
					result = "等级分数区间，ID：" + id + "，删除失败，请联系管理员！";
					log.error(result, e);
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return result;
				}
  		}
  		if (checkstatus.size() > 0 && checkstatus != null) {
  			result = Joiner.on(",").join(checkstatus);
  		}
  		return result;

    }

    /**
     * 根据绩效模板删除绩效等级分数区间
     *
     * @param levelTemplateId 绩效模板id
     */
    @Override
    public void deletePerfLevelScoreByLevelTemplateId(Long levelTemplateId) {
        perfLevelScoreMapper.deletePerfLevelScoreByLevelTemplateId(levelTemplateId);
    }
}
